package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author :XingJiaCheng
 * @date :2022-20-16-15:20
 * @description
 **/
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(
            HttpServletRequest request,
            @RequestBody Employee employee) {
        log.info("[登录]   用户名:{},密码{}", employee.getUsername(), employee.getPassword());

        LambdaQueryWrapper<Employee> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee dbEmployee = employeeService.getOne(QueryWrapper);
        if (dbEmployee == null) {
            log.info("");
            return R.error("没有该员工的信息");
        }

        String employeePassword = employee.getPassword();
        String dbEmployeePassword = dbEmployee.getPassword();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(employeePassword.getBytes());
        if (!md5DigestAsHex.equals(dbEmployeePassword)) {
            return R.error("您输入的密码不正确");
        }
        if (dbEmployee.getStatus() == 0) {
            return R.error("该员工出入软禁状态");
        }
        request.getSession().setAttribute("employee", dbEmployee.getId());


        log.info("登录成功啦");
        return R.success(dbEmployee);


    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.success("退出成功啦");

    }

    @PostMapping
    public R<String> saveEmployee(
            HttpServletRequest request,
            @RequestBody Employee employee
    ) {
        log.info("新增员工:{}", employee);
        String md5DigestAsHex = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(md5DigestAsHex);
        LocalDateTime now = LocalDateTime.now();
        employee.setCreateTime(now);
        employee.setUpdateTime(now);
        long LoginUserId = (long) request.getSession().getAttribute("employee");
        employee.setCreateUser(LoginUserId);
        employee.setUpdateUser(LoginUserId);
        employeeService.save(employee);
        log.info("新增员工:线程Id:{} emp:{}", Thread.currentThread().getId(),employee);
        return R.success("添加成功");

    }

    @GetMapping("/page")
    public R<Page> findByPage(Integer page, Integer pageSize, String name) {
        log.info("page:{},pageSize:{},name:{}",page,pageSize,name);
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Employee::getUpdateTime);
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);

        employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);

    }

    @PutMapping
    public R<String> update(HttpServletRequest request,
                            @RequestBody Employee Employee) {
        log.info("更新员工:{}", Employee);
        Employee.setUpdateTime(LocalDateTime.now());
        long loginUserId = (long) request.getSession().getAttribute("employee");
        Employee.setUpdateUser(loginUserId);
        employeeService.updateById(Employee);
        return R.success("员工信息修改成功");

    }

    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable long id) {
        log.info("查询的id:{}", id);
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return R.error("用户不存在");
        }
        log.info("查询成功了哦");
        return R.success(employee);


    }


}
